package com.MonkWorks.timedormitory;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.MonkWorks.timedormitory.R;
import com.MonkWorks.timedormitoryDB.ItimeProvider;
import com.MonkWorks.timedormitoryDB.TimeDB;

public class AdminToolsActivity extends Activity {

	private Button button_Create;
	private Button button_Logout;
	private Button button_Edit;
	private Button button_Manage;
	private Spinner job_Select;
	private String jobValue;

	ItimeProvider timeDB;

	private void initComponents() {
		timeDB = new TimeDB(this);

		button_Create = (Button) findViewById(R.id.btnCreate);
		button_Create.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent CIntent = new Intent(v.getContext(),
						CreateUserActivity.class);
				startActivityForResult(CIntent, 0);
			}

		});

		button_Edit = (Button) findViewById(R.id.btnEdit);
		button_Edit.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent CIntent = new Intent(v.getContext(),
						EditUserActivity.class);
				startActivityForResult(CIntent, 0);
			}

		});
		button_Manage = (Button) findViewById(R.id.btnEditTime);
		button_Manage.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent CIntent = new Intent(v.getContext(),
						SummaryActivity.class);
				CIntent.putExtra("JobValue", jobValue);
				startActivityForResult(CIntent, 0);

			}
		});
		
		button_Logout = (Button) findViewById(R.id.btnLogOut);
		button_Logout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent CIntent = new Intent(v.getContext(),
						LoginActivity.class);
				startActivityForResult(CIntent, 0);
				popup("You have logged out");
			}
		});

		job_Select = (Spinner) findViewById(R.id.jobSpinner);
		// spinner stuff
		List<String> fetchJob;
		
		try {
			fetchJob = timeDB.fetchAllJobs();
			Log.i("PunchActivity", "fetchJob for Spinner");
			ArrayAdapter jobAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_dropdown_item_1line, fetchJob);

			// finalize the values that go into the spinner
			job_Select.setAdapter(jobAdapter);

		} catch (Exception e) {
			popup("unable to populate spinner");
			Log.e("PunchActivity", "FetchJob");
		}
		job_Select.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				jobValue = job_Select.getSelectedItem().toString();

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		
		//make the commit button dissapear if no job is found
		if(jobValue != null)
		{
			button_Manage.setVisibility(0);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_tools);
		initComponents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_admin_tools, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    
	      //Logs the user out
	      if (item.getItemId() == R.id.Logoff) {
	    	  
	    	  Intent  MIntent = new Intent(getBaseContext(),LoginActivity.class);
				startActivityForResult(MIntent, 0);
				popup("You have logged out");
	        return true;
	      }


	      return (super.onOptionsItemSelected(item));
	    }

	protected void popup(String message) {
		// this is how we make a popup.
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
}
